import request from '/@/utils/request';
import {ref,toRefs,ToRefs} from 'vue'

export function dictApi() {
    return {
        // 根据字典类型查询字典数据信息
        getDicts(types:string) {
            return request({
                url: '/system/dict/mapByType',
                method: 'get',
                params:{types:types}
            })
        },
        //获取字典数据
        useDict(...args:string[]):ToRefs<any>{
            const res:any = ref({});
            args.forEach((d:string) => {
                res.value[d] = [];
            })
            dictApi().getDicts(args.join()).then(resp => {
                for (let key in resp.data) {
                    res.value[key] = resp.data[key]
                }
            })
            return toRefs(res.value);
        },
        page(data:any) {
            return request({
                url: '/system/dict/page',
                method: 'get',
                params:data
            })
        },
        listType() {
            return request({
                url: '/system/dict/listGroupByType',
                method: 'get',
            })
        },
        save(data:any) {
            return request({
                url: '/system/dict/save',
                method: 'post',
                data
            })
        },
        update(data:any) {
            return request({
                url: '/system/dict/update',
                method: 'post',
                data
            })
        },
        del(id:string) {
            return request({
                url: '/system/dict/delete',
                method: 'post',
                data: {id:id}
            })
        }
    }
}